from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
# from .models import Person
# Create your views here.

def index(request):
    #user = User.objects.create_user(username='kangbazi',password='91wangfan',email="91wangfan@qq.com")
    # user = User.objects.get(pk=1)
    # user.set_password("91daxiong")
    # user.save()

    # username = 'kangbazi'
    # password = '91daxiong'
    # user = authenticate(request,username=username,password=password)
    # if user:
    #     print(user)
    # else:
    #     print("用户名或者密码错误")
    # user = User.objects.create_superuser(username='91wangfan',password='1301231',email="123@qq.com")
    # user.save()
    # user = User.objects.create_user(telephone='17777777777',username='123qweasd',password="123123123!")
    # user.save()
    return HttpResponse("OK")


#一对一外键 扩展
def one_view(request):
    # user = User.objects.create_user(username="jiangjie",email="jiejie@qq.com",password="123456")
    user = User.objects.create_user(username="jiangjie1",email="jiejie1@qq.com",password="123456")
    user.extension.telephone = '18999999999'
    user.save()

    return HttpResponse("one to one ")





#代理扩展
def proxy_view(request):
    # blacklist = Person.get_blacklist()
    # for bla in blacklist:
    #     print(bla.username)
    return HttpResponse("PROXY")

# 验证用户的密码
def my_authenticate(username,password):
    user = User.objects.filter(username=username).first()
    if user:
        is_correct = user.check_password(password)
        if is_correct:
            return user
        else:
            return None
    else:
        return None
#手机号验证   一对一扩展模型
def my_authenticate_extension(telephone,password):
    user = User.objects.filter(extension__telephone=telephone).first()
    if user:
        is_correct = user.check_password(password)
        if is_correct:
            return user
        else:
            return None
    else:
        return None

def test_view(request):
    username = request.GET.get('username')
    password = request.GET.get('password')
    user = my_authenticate(username,password)
    if user:
        print("%s验证成功" % user.username)
    else:
        print("验证失败")
    return HttpResponse("ojok")

def test_extension_view(request):
    username = request.GET.get('telephone')
    password = request.GET.get('password')
    user = my_authenticate_extension(username,password)
    if user:
        print("%s验证成功" % user.username)
    else:
        print("验证失败")
    return HttpResponse("ojok")
